from iamadb import iamadb, thread, author, conversation
from ftplib import FTP
from boe2.reddit import *

reddit = client.client()
articles = reddit.getSubReddit("wildstar", limit=10)
comments = {}
conversations = {}
threadid = ""

def addComments(commentlist):
    for comment in commentlist:
        comments[comment.fullname()] = comment
        addComments(comment.replies)
        
def createConversation(comment):
    conv = conversation()
    conv.thread = thr.id
    conv.q_id = comment.parent_id
    conv.q_author = comments[comment.parent_id].author
    conv.q_body = comments[comment.parent_id].body
    conv.q_posted = comments[comment.parent_id].created
    conv.a_id = comment.fullname()
    conv.a_author = comment.author
    conv.a_body = comment.body
    conv.a_posted = comment.created
    conv.save(db)

with iamadb("iama.sqlite") as db:
    for article in articles.data:
        if "wildstar spellslinger class team" in article.title.lower():
            thr = thread()
            thr.id = article.fullname()
            threadid = thr.id
            thr.title = article.title
            thr.author = article.author
            thr.body = article.selftext
            thr.posted = article.created
            thr.last_update = article.edited
            thr.url = article.permalink
            thr.save(db)
            print("Saving comments for " + thr.id + "...")
            commentlist = reddit.getComments(article.permalink)
            addComments(commentlist)
            for commentid, comment in comments.items():
                if comment.author.startswith('CRB_') and comment.parent_id is not None and not comment.parent_id.startswith("t3_"):
                    createConversation(comment)
print('Scraping done, generating html...')
baselink = "http://www.reddit.com"
csslink = "<link rel='stylesheet' type='text/css' href='iama.css'>"

ftp = FTP('ftp.boe2.be')
ftp.login('boeqjpkx', 'E8Yud4Tt')
ftp.cwd('www/iama')
with iamadb("iama.sqlite") as db:
    thread = db.getThread(threadid)
    conversations = db.getConversations(thread.id)
    filename = "".join([c for c in thread.title if c.isalpha() or c.isdigit() or c==' ']).rstrip().lower().replace(' ', '_')[:200] + ".html"
    f = open(filename, 'w')
    f.write("<!DOCTYPE html><html><head><meta charset='UTF-8' /><title>{0}</title>{1}</head><body><a class='title' href='{2}' target='_blank'>{0}</a><div id='intro'><pre>{3}</pre></div>".format(thread.title, csslink, baselink + thread.url, thread.body))
    for c in conversations:
        f.write("""<div class='conversation'>
        <div class='question'><pre class='question'>{0}</pre> ({1})<div class='link'><a href='{2}' target='_blank'>(link)</a></div></div>
        <div class='response'><pre>{3}</pre> ({4})<div class='link'><a href='{5}' target='_blank'>(link)</a></div></div>
        </div>""".format(c.q_body.replace('\n\n', '\n'), c.q_author, baselink + thread.url + c.q_id[3:], c.a_body.replace('\n\n', '\n'), c.a_author, baselink + thread.url + c.a_id[3:]))
    f.write("</body></html>")
    f.close()
    print(filename + " generated.")
    ftp.storbinary('STOR '+ filename, open(filename, 'rb'))
    print(filename + " uploaded.")
        
print("Done!")
